<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\cms\controller\uploadData;

use plugin\saiadmin\basic\BaseController;
use app\cms\logic\uploadData\ProjectMeetingUploadDataLogic;
use app\cms\model\project_meeting_invitation\ProjectMeetingInvitationSpeaker;
use app\cms\validate\uploadData\ProjectMeetingUploadDataValidate;
use support\Log;
use support\Request;
use support\Response;

/**
 * 资料上传控制器
 */
class ProjectMeetingUploadDataController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new ProjectMeetingUploadDataLogic();
        $this->validate = new ProjectMeetingUploadDataValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([]);
        $query = $this->logic->search($where);
        $data = $this->logic->getList($query);
        return $this->success($data);
    }


    /**
     * 保存数据
     * @param Request $request
     * @return Response
     */
    public function save(Request $request): Response
    {
        $data = $request->post();

        if ($this->validate) {
            if (!$this->validate->scene('save')->check($data)) {
                return $this->fail($this->validate->getError());
            }
        }
        $result = $this->logic->save($data);
        if ($result) {
            $invitation_speaker_id = $data['invitation_speaker_id'];
            ProjectMeetingInvitationSpeaker::where('id', $invitation_speaker_id)->setInc('is_fill_info', 1);
        }

        Log::info('返回结果：' . json_encode($result));
        if ($result) {
            // $res = $this->afterChanges($data);
            $res = $this->logic->order('id', 'desc')->find();
            return $this->success(['id' => $res->id], '操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }
}
